package qf.wlh.com.oneprisdent;

import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.Vitamio;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;
import widget.CustomMediaController;

public class PlayVideoActivity extends AppCompatActivity implements MediaPlayer.OnInfoListener, MediaPlayer.OnBufferingUpdateListener{
    //视频地址id=
    private String path = "http://api.yixi.tv/youku.php?id=";
    private String name ;
    private Uri uri;
    private ProgressBar pb;
    private TextView downloadRateView, loadRateView;
    private MediaController mMediaController;
    private CustomMediaController mCustomMediaController;
    private VideoView mVideoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window = PlayVideoActivity.this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        //必须写这个，初始化加载库文件
        Vitamio.initialize(this);
        //设置视频解码监听
        if (!LibsChecker.checkVitamioLibs(this)) {
            return;
        }
        setContentView(R.layout.activity_play_video);
        //做初始化操作
        x.Ext.init(getApplication());

        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        initView();

        final Handler handler = new Handler();
        RequestParams params = new RequestParams(path);
        Log.e("TAG", "onCreate: "+path+name);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
//                result = result.substring(result.indexOf("http"),result.indexOf("\"",result.indexOf("http")+1));
                Log.e("AAA","==onSuccess=>"+result);
                //在此请求网络请求  去其你去播放视屏的网络路径
                String finalResult = null;
                switch (name){
                    case "XODg1MjI4NTUy":
                        finalResult  = "http://k.youku.com/player/getFlv" +
                                "Path/sid/8480054492479511f303a_01/st/mp4/file" +
                                "id/030020010054CFE855AB1007383F55854A22A1-DDB7-4AC8-" +
                                "6B79-710BDB3FE624?K=865c570c75c36964282be9bb&hd=0&ts=239" +
                                "3&ctype=51&token=4650&ev=1&oip=2021929295&ep=H2cMo8emAH853" +
                                "CGVPPyfmQwQ0Tddd1mWRvVG9Y8PHv%2F8prRIufBwIgVa1Wj44Qq1ZTbCEGJ" +
                                "GML1092uUoVxr0IK5q7Dhklqlzjzhqi2TQ%2Fijh7d95Ghiz3sds9KX57ix";
                        break;
                    case "XNjg2NjUwODk2":
                         finalResult = "http://k.youku.com/player/getFlvPath/sid/14800" +
                                 "5479085751ad6bc6_01/st/mp4/fileid/0300200100532B6B34B7200" +
                                 "7383F55D1CB022A-7E64-3A7A-D4C7-68EB94EFF054?K=0afcef8411642" +
                                 "b42261f5dcf&hd=0&ymovie=1&ts=1852&ctype=51&token=5925&ev=1&oip=20" +
                                 "21929295&ep=At%2BRKj8aE%2B8Mi4Qgxw%2Fd8Dw8wnSo1m4NRvVG9Y8PHv%2B9%2Fx" +
                                 "e0wt4vclmtgcJkOHUcRtnersUP8ZGWK%2BxZhXFB6UMjJiZZYsbmDVXfqEoz%2B0VdXZE" +
                                 "hZzKlECxnLHaY%2FKKj";
                        break;
                    case "XMTU5ODM3MDIyMA==":
                        finalResult  = "http://k.youku.com/player/getFlvPath/sid/1480487555" +
                                "82851df8056_01/st/mp4/fileid/0300200100578F7665998807383F555D72E24" +
                                "1-4ACC-0AA7-CF1D-6BC10D407FDB?K=b30e78f0b3f54085261f6028&hd=0&ymovie=1&" +
                                "ts=1758&ctype=51&token=1292&ev=1&oip=2021929295&ep=0ddW5WoUMXfFDg4oR%2Bk" +
                                "0VnSXwoWUg1szRvVG9Y8" +
                                "PHv%2FUTxRPV%2FrYHpapImAAusB1xEg5alxZNvBM542FZWvzh79KoI7q%2Frd4oWXyFb%2BhRP7oV" +
                                "91IxLlwpTNDSRpYs%2Fa8";
                        break;
                }
                final String finalResult1 = finalResult;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        initData(finalResult1);
                    }
                });
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("AAA","==onError=>");
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("AAA","==onCancelled=>");
            }

            @Override
            public void onFinished() {
                Log.e("AAA","==onFinished=>");
            }
        });
    }

    //初始化控件
    private void initView() {
        mVideoView = (VideoView) findViewById(R.id.buffer);
        mMediaController= new MediaController(this);
        mCustomMediaController=new CustomMediaController(this,mVideoView,this);
        mCustomMediaController.setVideoName("");
        pb = (ProgressBar) findViewById(R.id.probar);
        downloadRateView = (TextView) findViewById(R.id.download_rate);
        loadRateView = (TextView) findViewById(R.id.load_rate);
    }

    //初始化数据
    private void initData(String playPath) {
        uri = Uri.parse(playPath);
        mVideoView.setVideoURI(uri);//设置视频播放地址
        mVideoView.setMediaController(mCustomMediaController);
        mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);//高画质
        mMediaController.show(5000);
        mVideoView.requestFocus();
        mVideoView.setOnInfoListener(this);
        mVideoView.setOnBufferingUpdateListener(this);
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.setPlaybackSpeed(1.0f);
            }
        });
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        switch (what) {
            //缓冲的开始
            case MediaPlayer.MEDIA_INFO_BUFFERING_START:
                if (mVideoView.isPlaying()) {
                    mVideoView.pause();
                    pb.setVisibility(View.VISIBLE);
                    downloadRateView.setText("");
                    loadRateView.setText("");
                    downloadRateView.setVisibility(View.VISIBLE);
                    loadRateView.setVisibility(View.VISIBLE);

                }
                break;
            //缓冲100%
            case MediaPlayer.MEDIA_INFO_BUFFERING_END:
                mVideoView.start();
                pb.setVisibility(View.GONE);
                downloadRateView.setVisibility(View.GONE);
                loadRateView.setVisibility(View.GONE);
                break;
            case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED://下载的速率
                downloadRateView.setText("" + extra + "kb/s" + "  ");
                break;
        }
        return true;
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        loadRateView.setText(percent + "%");
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        //屏幕切换时，设置全屏
        if (mVideoView != null){
            mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_SCALE, 0);
        }
        super.onConfigurationChanged(newConfig);
    }
}
